home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / comm / wnos5src.zip / TRACE.C < prev    next >
C/C++ Source or Header  |  1993-10-05  |  6KB  |  276 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <stdarg.h>
  4. #include <conio.h>
  5. #include "global.h"
  6. #include "mbuf.h"
  7. #include "iface.h"
  8. #include "trace.h"
  9. #include "pktdrvr.h"
  10. #include "commands.h"
  11. #include "session.h"
  12. #include "files.h"
  13.  
  14. #define ASCIIBUF    80
  15. #define HEXBUF      16
  16.  
  17. void
  18. dump(struct iface *iface,int direction,unsigned type,struct mbuf *bp)
  19. {
  20.     int16 size;
  21.     struct mbuf *tbp;
  22.     char *cp;
  23.     void (*func) __ARGS((FILE *,struct mbuf **,int));
  24.  
  25.     if(iface == NULLIF) {
  26.         return;
  27.     }
  28.     switch(direction) {
  29.     case IF_TRACE_IN:
  30.         iface->lastrecv = secclock();
  31.         iface->rawrecvcnt++;
  32.         break;
  33.     case IF_TRACE_OUT:
  34.         iface->lastsent = secclock();
  35.         iface->rawsndcnt++;
  36.         break;
  37.     }
  38.     if((Current != Trace && iface->trfile == NULLCHAR)
  39.       || (iface->trace & direction) == 0) {
  40.         /* Nothing to trace */
  41.         return;
  42.     }
  43.     if(direction == IF_TRACE_IN
  44.       && (iface->trace & IF_TRACE_NOBC)
  45.       && (Tracef[type].addrtest != NULLFP)
  46.       && (*Tracef[type].addrtest)(iface,bp) == 0) {
  47.         return;
  48.     }
  49.     cp = ctime(&currtime);
  50.     cp[24] = '\0';
  51.     
  52.     trprintf(iface->trfp,"%s %s (%s):\n",
  53.         iface->name,(direction & IF_TRACE_OUT) ? "sent" : "recv",cp);
  54.  
  55.     if(bp == NULLBUF || (size = len_p(bp)) == 0) {
  56.         trprintf(iface->trfp,"empty packet\n");
  57.         return;
  58.     }
  59.     dup_p(&tbp,bp,0,size);
  60.  
  61.     if(tbp != NULLBUF) {
  62.         if(iface->trace & IF_TRACE_RAW) {
  63.             goto jp;
  64.         }
  65.         if((func = (type < NCLASS) ? Tracef[type].tracef : NULLVFP) != NULLVFP) {
  66.             textattr(WHITE);
  67.             (*func)(iface->trfp,&tbp,1);
  68.             textattr(LIGHTGRAY);
  69.  
  70.             if(iface->trace & IF_TRACE_HEX) {
  71.                 char buf[HEXBUF];
  72.                 int len, address = 0;
  73. jp:
  74.                 free_p(tbp);
  75.                 dup_p(&tbp,bp,0,len_p(bp));
  76.  
  77.                 /* Dump entire packet in hex/ascii */
  78.                 while((len = pullup(&tbp,buf,HEXBUF)) != 0) {
  79.                     static char hex[] = "0123456789abcdef";
  80.                     char line[81], *cp = line, *aptr = &line[6], *cptr = &line[55], c;
  81.  
  82.                     memset(line,' ',sizeof(line));
  83.  
  84.                     *cp++ = hex[hinibble(hibyte(address))];
  85.                     *cp++ = hex[lonibble(hibyte(address))];
  86.                     *cp++ = hex[hinibble(lobyte(address))];
  87.                     *cp   = hex[lonibble(lobyte(address))];
  88.  
  89.                     address += len;
  90.                     cp = buf;
  91.  
  92.                     while(len-- != 0) {
  93.                         c = *cp++;
  94.                         *aptr++ = hex[hinibble(uchar(c))];
  95.                         *aptr   = hex[lonibble(uchar(c))];
  96.                         aptr += 2;
  97.                         c &= 0x7f;
  98.                         *cptr++ = isprint(uchar(c)) ? c : '.';
  99.                     }
  100.                     *cptr++ = '\n';
  101.                     *cptr   = '\0';
  102.  
  103.                     trprintf(iface->trfp,"%s",line);
  104.                 }
  105.             } else if(iface->trace & IF_TRACE_ASCII) {
  106.                 /* Dump only data portion of packet in ascii */
  107.                 char buf[ASCIIBUF];
  108.                 int len;
  109.                 unsigned char c = 13;
  110.  
  111.                 while((len = pullup(&tbp,buf,ASCIIBUF)) != 0) {
  112.                     char *cp = buf;
  113.                     char *cpw = buf;
  114.                     while(len-- != 0) {
  115.                         switch(c = *cp) {
  116.                         default:
  117.                             if(c >= 0x20) {
  118.                                 *cp++ = c;
  119.                             } else {
  120.                                 *cp++ = '.';
  121.                             }
  122.                             break;
  123.                         case '\n':
  124.                         case '\r':
  125.                             *cp++ = '\0';
  126.                             trprintf(iface->trfp,"%s\n",cpw);
  127.  
  128.                             if(*cp) {
  129.                                 cpw = cp;
  130.  
  131.                                 if((c == '\r' && *cpw == '\n')
  132.                                   || (c == '\n' && *cpw == '\r')) {
  133.                                     cpw++;
  134.                                     cp++;
  135.                                     c = 13;
  136.                                     len--;
  137.                                 }
  138.                             } else {
  139.                               *cpw = '\0';
  140.                             }
  141.                             break;
  142.                         }
  143.                     }
  144.                     if(*cpw) {
  145.                         *cp = '\0';
  146.                         trprintf(iface->trfp,"%s",cpw);
  147.                     }
  148.                 }
  149.                 if(c != 13) {
  150.                     trprintf(iface->trfp,"\n");
  151.                 }
  152.             }
  153.         }
  154.         free_p(tbp);
  155.     } else {
  156.         trprintf(iface->trfp,"No space!\n");
  157.     }
  158. }
  159.  
  160. static void near
  161. d_trace(struct iface *ifp)
  162. {
  163.     tprintf("%s: ",ifp->name);
  164.  
  165.     if(ifp->port) {
  166.       tputs("Trace on master iface only");
  167.     } else {
  168.         if(ifp->trace & (IF_TRACE_IN | IF_TRACE_OUT | IF_TRACE_RAW)) {
  169.             if(ifp->trace & IF_TRACE_IN) {
  170.                 tputs("input ");
  171.             }
  172.             if(ifp->trace & IF_TRACE_OUT) {
  173.                 tputs("output ");
  174.             }
  175.             if(ifp->trace & IF_TRACE_NOBC) {
  176.                 tputs("- no broadcasts ");
  177.             }
  178.             if(ifp->trace & IF_TRACE_HEX) {
  179.                 tputs("(Hex/ASCII dump) ");
  180.             } else if(ifp->trace & IF_TRACE_ASCII) {
  181.                 tputs("(ASCII dump) ");
  182.             } else {
  183.                 tputs("(headers only) ");
  184.             }
  185.             if(ifp->trace & IF_TRACE_RAW) {
  186.                 tputs("Raw output ");
  187.             }
  188.             if(ifp->trfile != NULLCHAR) {
  189.                 tprintf("trace file: %s",ifp->trfile);
  190.             }
  191.         } else {
  192.             tputs("tracing off");
  193.         }
  194.     }
  195.     tputs("\n");
  196. }
  197.  
  198. /* Modify or displace interface trace flags */
  199. int
  200. dotrace(int argc,char **argv,void *p)
  201. {
  202.     struct iface *ifp;
  203.  
  204.     if(argc < 2){
  205.         for(ifp = Ifaces; ifp != NULLIF; ifp = ifp->next) {
  206.             d_trace(ifp);
  207.         }
  208.     } else {
  209.         if((ifp = if_lookup(argv[1])) == NULLIF){
  210.             tprintf(Badif,argv[1]);
  211.             return 1;
  212.         }
  213.         if(argc == 2) {
  214.             d_trace(ifp);
  215.             return 0;
  216.         }
  217.         if(argc >= 3) {
  218.             ifp->trace = htoi(argv[2]);
  219.         }
  220.         if(ifp->trfp != NULLFILE) {
  221.             Fclose(ifp->trfp);
  222.         }
  223.         ifp->trfp = NULLFILE;
  224.  
  225.         if(ifp->trfile != NULLCHAR) {
  226.             xfree(ifp->trfile);
  227.         }
  228.         ifp->trfile = NULLCHAR;
  229.  
  230.         if(argc >= 4){
  231.             if((ifp->trfp = Fopen(argv[3],APPEND_TEXT,0,1)) != NULLFILE) {
  232.                 ifp->trfile = strxdup(argv[3]);
  233.             }
  234.         }
  235.     }
  236.     return 0;
  237. }
  238.  
  239. /* shut down all trace files */
  240. void
  241. shuttrace(void)
  242. {
  243.     struct iface *ifp;
  244.  
  245.     for(ifp = Ifaces; ifp != NULLIF; ifp = ifp->next) {
  246.         if(ifp->trfp != NULLFILE) {
  247.             Fclose(ifp->trfp);
  248.         }
  249.         if(ifp->trfile != NULLCHAR) {
  250.             xfree(ifp->trfile);
  251.         }
  252.         ifp->trfile = NULLCHAR;
  253.         ifp->trfp = NULLFILE;
  254.     }
  255. }
  256.  
  257. void
  258. trprintf(FILE *fp,char *fmt,...)
  259. {
  260.     va_list ap;
  261.     char tmp[LINELEN];
  262.  
  263.     va_start(ap,fmt);
  264.     vsprintf(tmp,fmt,ap);
  265.     va_end(ap);
  266.  
  267.     if(Current == Trace) {
  268.         if(cputs(tmp) == '\n') {
  269.             cputs("\r");
  270.         }
  271.     }
  272.     if(fp != NULLFILE) {
  273.         fputs(tmp,fp);
  274.     }
  275. }
  276.